//package com.extern.controller;
//
//import com.alibaba.fastjson.JSONObject;
//import com.rocketmq.JmsProperties;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
//import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
//import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
//import org.apache.rocketmq.client.exception.MQClientException;
//import org.apache.rocketmq.common.message.MessageExt;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.stereotype.Component;
//
///**
// * @author: hw
// * @date: 2021/1/18 15:08
// */
//@Component
//@Slf4j
//public class SyncConsumer {
//
//
//     private DefaultMQPushConsumer consumer;
//
//    @Autowired private JmsProperties jms;
//
//
//    @Bean
//     public int SyncConsumer() throws MQClientException {
//         //设置消费组
//         consumer = new DefaultMQPushConsumer(jms.getSyncProducerGroup());
//         // 添加服务器地址
//         consumer.setNamesrvAddr(jms.getServerAddr());
//         // 添加订阅号(主题)
//         consumer.subscribe(jms.getTopic(), "sync");
//         // 监听消息
//         consumer.registerMessageListener((MessageListenerConcurrently) (msgs, context) -> {
//             MessageExt msg = msgs.get(0);
//             String message = new String(msgs.get(0).getBody());
//             JSONObject jsonObject = JSONObject.parseObject(message);
//             Integer productId = jsonObject.getInteger("productId");
//             Integer total = jsonObject.getInteger("total");
//             String key = msg.getKeys();
//             log.info("同步消费端消费消息，商品ID={},销售数量={}",productId,total);
//             try {
//                 log.info("同步消费者/消费完成");
//                 return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
//             } catch (Exception e) {
//                 log.info("同步消费失败，进行重试，重试到一定次数 那么将该条记录记录到数据库中，进行如果处理");
//                 e.printStackTrace();
//                 return ConsumeConcurrentlyStatus.RECONSUME_LATER;
//             }
//         });
//
//         consumer.start();
//         System.out.println("consumer start ...");
//         return 1;
//     }
//}
